Timing Attacks
Here is the golden rule of writing code that is “constant-time”:
Secret information may only be used in an input to an instruction if that input has no impact on what resources will be used and for how long.
In practice, this golden rule has a few implications:
Secret values cannot be used to decide what code to execute next (i.e. it cannot be used as a condition to a branch instruction)
Secret values cannot be used to decide what memory address to access.
Secret values cannot be used as input to variable-time instructions like DIV on x86 (smaller numbers divide faster).
Refs
A beginner's guide to constant-time cryptography
Comprehensive timing leak protection for Rust programs
Timing Attacks on ECDSA, ECDHE, AES and SHA2
Security of rust-timing-shield
Tools
Subtle: Pure-Rust traits and utilities for constant-time cryptographic implementations.
Popular examples
curve25519 secret keyの2^254 positionは常に1にset
montgomery ladderにおけるmulでtiming attackを防ぐ
most significant bitsの探索で生じるtiming leakage問題
常にbit fixedしておくことで対処
code:1
for(int i = log2(exponent) - 2; i >= 0; --i)
{
}
Audit reports
条件分岐によりb > aかどうかのtiming leakの可能性。
https://gyazo.com/a141eb80db631313eb8a3a94cb2abff6
https://gyazo.com/de80e2cf211b527351f24603ac8094a1